import { defineStore } from 'pinia';
import { getToken, getOpenid, getCurrentCity, clearAuthStorage } from './helpers';
import { localStg } from '@/utils';
interface AuthState {
  /** 用户信息 */
  nurseUser: NurseUser.nurseUser;
  /** 用户token */
  token: string;

  openid: string;

  currentCity: string;

  /** 当前选中的被监护人 */
  curFamily: NurseUser.NurseUserFamily | null;

}



export const useAuthStore = defineStore('auth-store', {
  state: (): AuthState => ({
    nurseUser: localStg.get('nurseUser') || {
      id: '',
      name: '',
      phone: '',
      sex: '',
      openid: '',
      nurseUserFamilyList: [],
      nurseUserAddrList: [],
      defaultHospital: '1',
      defaultHospitalName: '南宁安好护理站',
      defaultHospitalLatitude:'22',
      defaultHospitalLongitude: '42',
      mobile:'',//手机
      idCard: '',//身份证
      face: '',
      healthCode: '',
      vuid: '',//健康码
      isPhone: '',
      isVerified: '',
    },
    token: getToken(),

    openid: getOpenid(),

    currentCity: getCurrentCity(),

    curFamily: localStg.get('curFamily') || null,
  }),
  getters: {
    /** 是否登录 */
    isLogin: state => Boolean(state.openid),
    // getNurseUser: (state) => () => state.nurseUser
    isRegistered: (state) => {
      // const isHuaSheng = state.nurseUser.defaultHospitalName?.includes('华声');
      // if(isHuaSheng && state.nurseUser.vuid){
      //   return true
      // }
      if(state.nurseUser.isVerify == '1'){
        return true
      }
      if(state.nurseUser.isPhone && state.nurseUser.idcard){
        return true
      }
      return false
    }
  },
  actions: {
    async setNurseUser(nurseUser: NurseUser.nurseUser) {
      
      // 处理 nurseUserAddrList
      const list = nurseUser.nurseUserAddrList?.map((item) => {
        return {
          ...item,
          fullAddress: `${item.areaName}${item.address}`,
          mobile: '',
          idCard: '',
          face: '',
          healthCode: '',
        }
      })
      if (list && list.length > 0) nurseUser.nurseUserAddrList = [...list]
      

      // 确保其他数组字段也是数组，但不要清空现有数据
      if (!Array.isArray(nurseUser.nurseUserFamilyList)) {
        nurseUser.nurseUserFamilyList = []
      }
      if (!Array.isArray(nurseUser.nurseUserAddrList)) {
        nurseUser.nurseUserAddrList = []
      }
      if (!Array.isArray(nurseUser.nurseHomeAddrList)) {
        nurseUser.nurseHomeAddrList = []
      }
      
      this.nurseUser = nurseUser;
      localStg.set("nurseUser", nurseUser)


      const { useHospitalStore } = await import('@/store');
      const hospital = useHospitalStore()
      hospital.setDefaultHospitalById(nurseUser.defaultHospital || '')
    },
    setToken(token: string) {
      this.token = token;
      localStg.set("token", token)
    },
    setOpenid(openid: string) {
      this.openid = openid;
      localStg.set("openid", openid)
    },
    setCurrentCity(currentCity: string) {
      this.currentCity = currentCity;
      localStg.set("currentCity", currentCity)
    },

    /** 设置当前选中的被监护人 */
    setCurFamily(family: NurseUser.NurseUserFamily | null) {
      this.curFamily = family;
      localStg.set("curFamily", family);
    },

    /** 重置auth状态 */
    resetAuthStore() {
      clearAuthStorage();
      this.$reset();
    }
  }
});
